﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BussinessLayer;
using DataAccessLayer.Entity;
namespace QLTSLienThong.Form_he_thong
{
    public partial class frmPhongThi : Form
    {
        public frmPhongThi()
        {
            InitializeComponent();
        }
        PhongThiBussiness phongBussiness = new PhongThiBussiness();
        clsPhongThi clsPhong;
        bool _KiemTraThem, KiemTraTonTaiMa=true;
        object kq;
        private void frmPhongThi_Load(object sender, EventArgs e)
        {
            LoadPhongThi();
        }
        private void LoadPhongThi()
        {
            dtgPhongThi.DataSource = phongBussiness.LoadPhongThi();
            LoadControl();
            KhoaText();
        }
        private void LoadControl()
        {
            txtMaP.DataBindings.Clear();
            txtMaP.DataBindings.Add("Text",dtgPhongThi.DataSource,"MaPhong");
            txtTenP.DataBindings.Clear();
            txtTenP.DataBindings.Add("Text", dtgPhongThi.DataSource, "TenPhong");
        }
        private void KhoaText()
        {
            txtMaP.ReadOnly = true;
            txtTenP.ReadOnly = true;
            btnThemPhong.BringToFront();
            btnSuaPhong.BringToFront();
        }
        private void MoText()
        {
            txtMaP.ReadOnly = false;
            txtTenP.ReadOnly = false;
            btnThemPhong.SendToBack();
            btnSuaPhong.SendToBack();
            txtMaP.Focus();
        }
        private void XoaText()
        {
            txtMaP.Clear();
            txtTenP.Clear();
        }
        private void btnHuyPhong_Click(object sender, EventArgs e)
        {
            LoadPhongThi();
        }

        private void btnLuuPhong_Click(object sender, EventArgs e)
        {
            if (KiemTraDuLieu())
            {
                if (_KiemTraThem)
                {
                    clsPhong = new clsPhongThi();
                    clsPhong.MaPhong = txtMaP.Text.Trim();
                    clsPhong.TenPhong = txtTenP.Text.Trim();
                    phongBussiness.ThemPhong(clsPhong);
                    MessageBox.Show("Thêm thành công!","Thông báo!");
                    LoadPhongThi();
                }
                else
                {
                    clsPhong = new clsPhongThi();
                    clsPhong.MaPhong = txtMaP.Text.Trim();
                    clsPhong.TenPhong = txtTenP.Text.Trim();
                    phongBussiness.SuaPhong(clsPhong);
                    MessageBox.Show("Sửa thành công!", "Thông báo!");
                    LoadPhongThi();
                }
            }
        }

        private void btnSuaPhong_Click(object sender, EventArgs e)
        {
            _KiemTraThem = false;
            MoText();
        }

        private void btnThemPhong_Click(object sender, EventArgs e)
        {
            _KiemTraThem = true;
            MoText();
            XoaText();
        }

        private void btnXoaPhong_Click(object sender, EventArgs e)
        {
            XoaPhong();
        }
        private bool KiemTraDuLieu()
        {
            if (txtMaP.Text.Trim() == "")
            {
                MessageBox.Show("Chưa nhập mã phòng!","Thông báo");
                txtMaP.Focus();
                return false;
            }
            if (_KiemTraThem == false)
            {
                if (txtMaP.Text.Trim() == dtgPhongThi.CurrentRow.Cells["MaPhong"].Value.ToString())
                {
                    KiemTraTonTaiMa = false;
                }
            }
            if (KiemTraTonTaiMa == true)
            {
                clsPhong = new clsPhongThi();
                clsPhong.MaPhong = txtMaP.Text.Trim();
                kq = phongBussiness.KiemTraMa(clsPhong);
                if (Convert.ToInt32(kq) > 0)
                {
                    MessageBox.Show("Mã này đã tồn tài!", "Thông báo!");
                    txtMaP.Focus();
                    return false;
                }
            }
            if (txtTenP.Text.Trim() == "")
            {
                MessageBox.Show("Chưa nhập tên phòng!", "Thông báo");
                txtTenP.Focus();
                return false;
            }
            return true;
        }
        private void XoaPhong()
        {
            if(MessageBox.Show("Bạn muốn xóa mã phòng: "+txtMaP.Text.Trim()+"?","Thông báo",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
            {
                    clsPhong = new clsPhongThi();
                    clsPhong.MaPhong = txtMaP.Text.Trim();
                    //clsPhong.TenPhong = txtTenP.Text.Trim();
                    phongBussiness.XoaPhong(clsPhong);
                    //MessageBox.Show("Sửa thành công!", "Thông báo!");
                    LoadPhongThi();
            }
        }
    }
}
